﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using MySql.Data.MySqlClient;
using eStock.SQL;

namespace eStock.DataObjects
{
    public class ComponentCategoryCollection : DataCollection<ComponentCategory>
    {
        public override ComponentCategory GetById(int id)
        {
            return List.Cast<ComponentCategory>().FirstOrDefault(c => c.Id == id);
        }

        public override void Replace(ComponentCategory newValue)
        {
            List[IndexOf(GetById(newValue.Id))] = newValue;
        }

        public List<ComponentCategory> GetChilds(int id)
        {
            return (from ComponentCategory c in List where c.ParentId == id select c).ToList();
        }

        public override bool Update()
        {
            try
            {
                SQLDataReader res;
                foreach (ComponentCategory c in List)
                {
                    res = MainWindow.Database.QueryRead("SELECT * FROM es_categories WHERE id = " + c.Id);
                    int i = MainWindow.Database.FetchCount(res);
                    if (i == 0)
                    {
                        ((ComponentCategory)List[List.IndexOf(c)]).Id = Convert.ToInt32(MainWindow.Database.QueryWrite("INSERT INTO es_categories (parentId, title, description) VALUES ('" + c.ParentId + "', '" + MainWindow.Database.EscapeString(c.Title) + "', '" + MainWindow.Database.EscapeString(c.Description) + "');"));
                    }
                    else
                    {
                        MainWindow.Database.QueryWrite("UPDATE es_categories SET parentId = '" + c.ParentId + "', title = '" + MainWindow.Database.EscapeString(c.Title) + "', description = '" + MainWindow.Database.EscapeString(c.Description) + "' WHERE id = " + c.Id + ";");
                    }
                }
                res = MainWindow.Database.QueryRead("SELECT * FROM es_categories");
                Dictionary<string, object> data = MainWindow.Database.FetchArray(res);
                List<int> removeIds = new List<int>();
                while (data != null)
                {
                    ComponentCategory c = GetById(Convert.ToInt32(data["id"]));
                    if (c == null)
                    {
                        removeIds.Add(Convert.ToInt32(data["id"]));
                    }
                    data = MainWindow.Database.FetchArray(res);
                }
                if (removeIds.Count > 0)
                {
                    string sqlString = "DELETE FROM es_categories WHERE ";
                    for (int j = 0; j < removeIds.Count; j++)
                    {
                        if (j != 0)
                            sqlString += " AND ";
                        sqlString += "id = " + removeIds[j];
                    }
                    MainWindow.Database.QueryWrite(sqlString);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
